package cn.dengta.webapp.live.entity;

import java.util.Date;

import cn.dengta.common.model.BizFlag;
import cn.dengta.common.model.Status;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;

/**
 * 直播主播表
 *
 * @author lgy
 * @since 2023-01-12
 */
@Setter
@Getter
@TableName("im_live_anchor")
public class LiveAnchor extends TimeBasedEntity implements BizFlag.WritableBizFlag {

	/** 标识：关闭PK邀请 */
	public static final long FLAG_PK_DISABLED = 1L;

	/** 用户ID */
	Long userId;
	/** 直播期间总的钻石数 */
	Long liveCoins;
	/** 冻结时间 */
	Date freezeTime;
	/** 推荐 */
	Integer recommend;
	/** 直播状态 */
	Integer liveStatus;
	/** 业务配置状态 */
	Long configFlag;
	/** 最近开播时间 */
	Date lastLiveTime;
	/** 最近开播结束时间 */
	Date lastLiveEndTime;
	/** 状态 */
	Integer status;

	public LiveAnchor init(LiveAnchor input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.freezeTime = X.expectNotNull(input.freezeTime, freezeTime);
		this.recommend = X.expectNotNull(input.recommend, recommend, 0);
		this.status = X.expectNotNull(input.status, status, Status.YES.value);
		this.liveStatus = X.expectNotNull(input.liveStatus, liveStatus, Status.NO.value);
		super.initTime(now);
		return this;
	}

	public boolean isFrozen() {
		return freezeTime != null && freezeTime.getTime() > System.currentTimeMillis();
	}

	public boolean isEnabled() {
		return status > 0;
	}

	public String getStatus_() {
		return canLive() ? "正常" : (isFrozen() ? "封禁" : null);
	}

	public boolean canLive() {
		return isEnabled() && !isFrozen();
	}

	public boolean liveOpen() {
		return Status.asResult(liveStatus);
	}

	public boolean forRecommend() {
		return Status.asResult(recommend);
	}

	/**
	 * 是否打开PK邀请
	 */
	public boolean allowPkInvite() {
		return !BizFlag.hasFlag(configFlag, FLAG_PK_DISABLED);
	}

	@Override
	public void setBizFlag(long flag) {
		this.configFlag = flag;
	}

	@Override
	public long getBizFlag() {
		return configFlag;
	}

	public static final String USER_ID = "user_id";
	public static final String LIVE_COINS = "live_coins";
	public static final String FREEZE_TIME = "freeze_time";
	public static final String RECOMMEND = "recommend";
	public static final String LIVE_STATUS = "live_status";
	public static final String LAST_LIVE_TIME = "last_live_time";
	public static final String LAST_LIVE_END_TIME = "last_live_end_time";
	public static final String STATUS = "status";

}
